unit uCep;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPai, DBCtrls, Grids, DBGrids, StdCtrls, Mask, FacDbEdit, DB,
  Buttons, ExtCtrls, ComCtrls;

const
  CM_FORMSETUP = WM_USER + 2;  

type
  TfCep = class(TfPai)
    FacDBEdit1: TFacDBEdit;
    Label1: TLabel;
    FacDBEdit4: TFacDBEdit;
    Label4: TLabel;
    FacDBEdit5: TFacDBEdit;
    Label5: TLabel;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    procedure FormShow(Sender: TObject);
    procedure btnIncluirClick(Sender: TObject);
    procedure FacDBEdit5Exit(Sender: TObject);
    procedure btngravarClick(Sender: TObject);
    procedure btnSairClick(Sender: TObject);
  private
    Procedure CMFormSetup(
      var Message: TMessage); message CM_FORMSETUP;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fCep: TfCep;

implementation

uses UDM, uInquilinos, uProprietarios, uClientes, uImovel_aluguel,
  uImovel_venda, uFiadores;

{$R *.dfm}

procedure TfCep.FormShow(Sender: TObject);
begin
  inherited;

   PostMessage(Handle, CM_FORMSETUP, 0, 0);

end;

procedure TfCep.btnIncluirClick(Sender: TObject);
begin
  inherited;
  FacDBEdit1.SetFocus;
end;

procedure TfCep.FacDBEdit5Exit(Sender: TObject);
begin
  inherited;
  if dm.cds_cep.State in [dsInsert, dsEdit] then
  begin
    btngravar.SetFocus;
  end
  else
  begin
    btnIncluir.SetFocus;
  end;

end;

procedure TfCep.btngravarClick(Sender: TObject);
begin
  inherited;
    btnIncluir.SetFocus;
end;

procedure TfCep.CMFormSetup(var Message: TMessage);
begin
 Update;

  if dm.cds_cep.Active then
     dm.cds_cep.Close;
  dm.cds_cep.Params[1].AsInteger:=9999999;
  dm.cds_cep.Open;

end;

procedure TfCep.btnSairClick(Sender: TObject);
begin
  inherited;
  if dm.cds_inquilino.Active then
  begin
    if dm.cds_inquilino.State in [dsBrowse] then
      dm.cds_inquilino.Edit;
      dm.cds_inquilinoCOD_ENDERECO.AsInteger := dm.cds_cepCOD_CEP.AsInteger;
      dm.cds_inquilinoRUA.AsString := dm.cds_cepRUA.AsString;
      dm.cds_inquilinoUF.AsString := dm.cds_cepUF.AsString;
      dm.cds_inquilinoCEP.AsString := dm.cds_cepCEP.AsString;
      fInquilinos.FacDBEdit2.SetFocus;
  end;
  if dm.cds_proprietario.Active then
  begin
    if dm.cds_proprietario.State in [dsBrowse] then
      dm.cds_proprietario.Edit;
      dm.cds_proprietarioCOD_ENDERECO.AsInteger := dm.cds_cepCOD_CEP.AsInteger;
      dm.cds_proprietarioRUA.AsString := dm.cds_cepRUA.AsString;
      dm.cds_proprietarioUF.AsString := dm.cds_cepUF.AsString;
      dm.cds_proprietarioCEP.AsString := dm.cds_cepCEP.AsString;
      fProprietarios.FacDBEdit2.SetFocus;
  end;
  if dm.cds_cliente.Active then
  begin
    if dm.cds_cliente.State in [dsBrowse] then
      dm.cds_cliente.Edit;
    dm.cds_clienteCOD_ENDERECO.AsInteger := dm.cds_cepCOD_CEP.AsInteger;
    dm.cds_clienteRUA.AsString := dm.cds_cepRUA.AsString;
    dm.cds_clienteUF.AsString := dm.cds_cepUF.AsString;
    dm.cds_clienteCEP.AsString := dm.cds_cepCEP.AsString;
    fClientes.FacDBEdit2.SetFocus;
  end;

  if dm.cds_imo_aluguel.Active then
  begin
    if dm.cds_imo_aluguel.State in [dsBrowse] then
      dm.cds_imo_aluguel.Edit;
    dm.cds_imo_aluguelCOD_ENDERECO.AsInteger := dm.cds_cepCOD_CEP.AsInteger;
    dm.cds_imo_aluguelRUA.AsString := dm.cds_cepRUA.AsString;
    dm.cds_imo_aluguelUF.AsString := dm.cds_cepUF.AsString;
    dm.cds_imo_aluguelCEP.AsString := dm.cds_cepCEP.AsString;
    fImovel_aluguel.FacDBEdit2.SetFocus;
  end;

  if dm.cds_imo_venda.Active then
  begin
    if dm.cds_imo_venda.State in [dsBrowse] then
      dm.cds_imo_venda.Edit;
    dm.cds_imo_vendaCOD_ENDERECO.AsInteger := dm.cds_cepCOD_CEP.AsInteger;
    dm.cds_imo_vendaRUA.AsString := dm.cds_cepRUA.AsString;
    dm.cds_imo_vendaUF.AsString := dm.cds_cepUF.AsString;
    dm.cds_imo_vendaCEP.AsString := dm.cds_cepCEP.AsString;
    fImovel_Venda.FacDBEdit3.SetFocus;
  end;

  if dm.cds_fiador.Active then
  begin
    if dm.cds_fiador.State in [dsBrowse] then
      dm.cds_fiador.Edit;
      dm.cds_fiadorCOD_ENDERECO.AsInteger := dm.cds_cepCOD_CEP.AsInteger;
      dm.cds_fiadorRUA.AsString := dm.cds_cepRUA.AsString;
      dm.cds_fiadorUF.AsString := dm.cds_cepUF.AsString;
      dm.cds_fiadorCEP.AsString := dm.cds_cepCEP.AsString;
      fFiadores.DBEdit2.SetFocus;
  end;

end;

end.
